package com.ruoyi.upload.service.impl;

import com.ruoyi.upload.service.FileService;
import com.ruoyi.util.QiniuUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Administrator
 * @version 1.0
 **/
@Service
public class FileServiceImpl implements FileService {

    @Value("${oss.qiniu.url}")
    private String qiniuUrl;
    @Value("${oss.qiniu.accessKey}")
    private String accessKey;
    @Value("${oss.qiniu.secretKey}")
    private String secretKey;
    @Value("${oss.qiniu.bucket}")
    private String bucket;

    /**
     * 上传文件
     *
     * @param bytes    文件字节数组
     * @param fileName 文件名
     * @return 文件访问路径（绝对的url）
     */
    @Override
    public String upload(byte[] bytes, String fileName, HttpServletRequest request) {

        //获取请求头
        String header = request.getHeader("Authorization");
        //分割图片名称
        String[] split = fileName.split("\\.");
        //拿到图片名称 和 请求头进行拼接
        String imagename = split[0];
        fileName = header + imagename;
        //调用common 下的工具类
//        String accessKey,String secretKey,String bucket, byte[] bytes,String fileName
        try {
            QiniuUtils.upload2qiniu(accessKey, secretKey, bucket, bytes, fileName);
        } catch (RuntimeException e) {
            e.printStackTrace();
//            throw new BusinessException(CommonErrorCode.E_100106);
        }
        //上传成功返回文件的访问地址（绝对路径）
        return fileName;
    }
}
